package Connection;

import Controllers.Application;
import Entities.Phase;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;


public class WebPhaseLoader implements PhaseLoader{
    private Application application;
    
    public WebPhaseLoader (Application application){
        this.application = application;
    }
    
    @Override
    public Phase loadPhase(int currentWorld, int currentPhase){
        try {
            URLConnection connection = getConnection();
        
            OutputStream outputStream = connection.getOutputStream();
            ObjectOutputStream o = new ObjectOutputStream(outputStream);
            o.writeObject("loadPhase");
            o.writeObject(currentWorld);
            o.writeObject(currentPhase);
            o.flush();
            o.close();
            
            InputStream inputStream = connection.getInputStream();
            ObjectInputStream i = new ObjectInputStream(inputStream);
            Phase phase = (Phase) i.readObject();
            
            i.close();
            return phase;
        } catch (Exception ex) {
            try {
                File file = new File("C:/PhaseLoader_loadPhase.txt");
                file.createNewFile();
            } catch (IOException ex1) {
                Logger.getLogger(FileWorldCreator.class.getName()).log(Level.SEVERE, null, ex1);
            }
            return null;
        }
    }
    
    @Override
    public ArrayList<String> loadImagePhaseList(int worldId){
        try{
            URLConnection connection = getConnection();
        
            OutputStream outputStream = connection.getOutputStream();
            ObjectOutputStream o = new ObjectOutputStream(outputStream);
            o.writeObject("loadImagePhaseList");
            o.writeObject(worldId);
            o.flush();
            o.close();
            
            InputStream inputStream = connection.getInputStream();
            ObjectInputStream i = new ObjectInputStream(inputStream);
            ArrayList<String> imagePhaseList = (ArrayList<String>) i.readObject();
            i.close();
           
            return imagePhaseList;
        }catch (Exception ex){
            try {
                File file = new File("C:/PhaseLoader_loadImagePhaseList.txt");
                file.createNewFile();
            } catch (IOException ex1) {
                Logger.getLogger(FileWorldCreator.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }
        return null;
    }
    
    public URLConnection getConnection(){
        URLConnection urlConnection = null;
        IPServer iPServer = new IPServer(application);
        String location = iPServer.getAdress() + "/ArkanoidServidor/PhaseServlet";
        try{
            URL url = new URL(location);
            urlConnection = url.openConnection();
            urlConnection.setDoInput(true);
            urlConnection.setDoOutput(true);
            urlConnection.setUseCaches(false);
            urlConnection.setRequestProperty("Content-Type", "application/x-java-serialized-object");
        } catch(Exception ex){
            try {
                File file = new File("C:/PhaseLoader_getConnection.txt");
                file.createNewFile();
            } catch (IOException ex1) {
                Logger.getLogger(FileWorldCreator.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }
        return urlConnection;
    }
}
